/* Turska Framework/UI library (Turs2)
 * Copyright 2003-2010 Jetro Lauha
 * All rights reserved.
 * Web: http://iki.fi/jetro/turska/
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of EITHER:
 *   (1) The GNU Lesser General Public License as published by the Free
 *       Software Foundation; either version 2.1 of the License, or (at
 *       your option) any later version. The text of the GNU Lesser
 *       General Public License is included with this library in the
 *       file LICENSE-LGPL.txt.
 *   (2) The BSD-style license that is included with this library in
 *       the file LICENSE-BSD.txt.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
 * LICENSE-LGPL.txt and LICENSE-BSD.txt for more details.
 *
 * $Id: T2VerticalStripBitmapFontSDLOpenGL.cpp 157 2010-01-01 23:31:43Z jlauha $
 * $Revision: 157 $
 */

#include "Turs2.h"

#ifdef T2_USE_SDLOPENGL

#include <assert.h>
#include "SDL.h"

namespace turska
{


VerticalStripBitmapFontSDLOpenGL::VerticalStripBitmapFontSDLOpenGL(GraphicsSDLOpenGL *graphics,
                                                                   const C8 *fileName,
                                                                   const C8 *fontString,
                                                                   UI8 alphaTraceMinAlpha) :
    VerticalStripBitmapFont(static_cast<Graphics *>(graphics), fileName, fontString, alphaTraceMinAlpha)
{
}


VerticalStripBitmapFontSDLOpenGL::~VerticalStripBitmapFontSDLOpenGL()
{
}


bool VerticalStripBitmapFontSDLOpenGL::reload()
{
    SDL_Surface *image;
    UI32 g, x, y, x1, x2;

    if (mLoaded && mGlyphs != 0)
    {
        UI32 a;
        for (a = 0; a < mGlyphCount; ++a)
            delete mGlyphs[a];
        delete[] mGlyphs;
        mGlyphs = 0;
        mGlyphCount = 0;
    }
    mLoaded = false;

    GraphicsSDLOpenGL *graphics = static_cast<GraphicsSDLOpenGL *>(mGraphics);
    image = graphics->loadSurface(mFileName);
    if (image == 0)
        return false;

    mGlyphCount = image->h / image->w;
    if (mGlyphCount < 1)
    {
        SDL_FreeSurface(image);
        return false;
    }
    mGlyphs = new Image *[mGlyphCount];

    mFontHeight = image->w;

    for (g = 0; g < mGlyphCount; ++g)
    {
        I32 w, h;
        SDL_LockSurface(image);
        x1 = image->w;
        x2 = 0;
        for (x = 0; (I32)x < image->w; ++x)
        {
            for (y = g * image->w; y < (g + 1) * image->w; ++y)
            {
                const Uint32 pixel = *(UI32 *)((UI8 *)image->pixels +
                    y * image->pitch + x * image->format->BytesPerPixel);
                Uint8 red, green, blue, alpha = 0;
                SDL_GetRGBA(pixel, image->format, &red, &green, &blue, &alpha);
                UI8 traceMinAlpha = mAlphaTraceMinAlpha;
                if ((image->flags & SDL_SRCALPHA) == 0)
                {
                    // TODO: Better handling of alpha when image doesn't have it.
                    alpha = red | green | blue;
                    traceMinAlpha = 0;
                }
                // trace right edge
                if (alpha > traceMinAlpha && x > x2)
                    x2 = x;
                // trace left edge
                if (alpha > traceMinAlpha && x < x1)
                    x1 = x;
            }
        }
        SDL_UnlockSurface(image);
        if (x1 > x2)
        {
            w = image->w / 3;
            h = image->w;
            x1 = 0;
            x2 = w;
        }
        else
        {
            w = x2 - x1 + 1;
            h = image->w;
        }

        PixelRectangle area;
        area.x = x1;
        area.y = g * image->w;
        area.width = w;
        area.height = h;
        
        // Convert the image into an OpenGL texture.
        // TODO - allow usage of nearest neighbor sampled fonts? -> API change..
        mGlyphs[g] = graphics->createImage(image, IF_FILTER_LINEAR, &area);
    }

    /* We don't need the original image anymore */
    SDL_FreeSurface(image);

    mLoaded = true;
    return mLoaded;
}


} // namespace turska
#endif // T2_USE_SDLOPENGL
